package com.poispace.util;

import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {   
        manyExcel("C:\\Users\\10491\\Downloads","C:\\test");
    }
    public static String [] getFileName(String path)
    {
        File file = new File(path);
        String [] fileName = file.list();
        return fileName;
    }
	private static void manyExcel(String readpath,String writepath) {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));
		String [] allname= getFileName(readpath);
        for(int i=0;i<allname.length;i++){
        	if(allname[i].substring(allname[i].lastIndexOf(".")+1).equals("xls")){
            MyTask myTask = new MyTask(i,allname[i],readpath,writepath);
            executor.execute(myTask);
            System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
            executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
            }
        }
        executor.shutdown();
	}
}


class MyTask implements Runnable {
   private int taskNum;
   private String  name;
   private String  readpath;
   private String  writepath;
   public MyTask(int num,String name,String readpath,String writepath) {
       this.taskNum = num;
       this.name = name;
       this.readpath = readpath;
       this.writepath = writepath;
   }
    
  
   public void run() {
//       System.out.println("正在执行task "+taskNum+"  ,文件名： "+name+"  ,读文件路径： "+readpath+"  ,写文件路径： "+writepath);
       try {
           Thread.currentThread().sleep(4000);
           OneExcelReader oer=new OneExcelReader();
           oer.readexcel(readpath, writepath, name);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       System.out.println("task "+taskNum+"执行完毕");
   }
}